package tools

import (
	"context"
	"fmt"
	"os/exec"
	"strings"
)

// ShellTool provides shell command execution capabilities
type ShellTool struct{}

// Name implements Tool
func (t *ShellTool) Name() string {
	return "shell"
}

// Description implements Tool
func (t *ShellTool) Description() string {
	return "Executes shell commands"
}

// Parameters implements Tool
func (t *ShellTool) Parameters() []Parameter {
	return []Parameter{
		{
			Name:        "command",
			Type:        "string",
			Description: "The shell command to execute",
			Required:    true,
		},
		{
			Name:        "timeout",
			Type:        "number",
			Description: "Command timeout in seconds",
			Required:    false,
			Default:     30,
		},
	}
}

// Execute implements Tool
func (t *ShellTool) Execute(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	command, ok := params["command"].(string)
	if !ok {
		return nil, fmt.Errorf("command parameter is required")
	}

	// Create command
	cmd := exec.CommandContext(ctx, "sh", "-c", command)

	// Capture output
	output, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("command failed: %v\nOutput: %s", err, string(output))
	}

	return strings.TrimSpace(string(output)), nil
}
